﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TomNet.IMSC.Core.Contracts.Common;
using TomNet.IMSC.Core.Contracts.Organization;
using TomNet.IMSC.Web.Common;
using TomNet.Web.WebApi.Filters;

using TomNet.Core.Dependency;

namespace TomNet.IMSC.Web.Filters
{
    public class ApiAuthenticationAttribute : BasicApiAuthenticationAttribute
    {
        //public IUsersLoginContract ULContract { get; set; }
        public IServiceProvider ServiceProvider { get; set; }
        
        public override bool ValidateToken(string token)
        {
            //获取请求路径
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            string[] urlArr = url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            string apiType = urlArr[urlArr.Length - 3].ToLower();
            string controlName = urlArr[urlArr.Length - 2].ToLower();
            string actionName = urlArr[urlArr.Length - 1].ToLower();


            if (apiType.Equals("staffapi"))//Staff验证
            {
                var SLContract = ServiceProvider.GetService<IStaffLoginContract>();
                token = token.Replace(' ', '+');
                string tRealitytoken = "";
                try
                {
                    tRealitytoken = BaseWebApiExt.GetRealityToken(token);//解密
                }
                catch (Exception)
                {
                    return false;
                }

                //验证内存中是否存在
                if (BaseWebApiExt.StaffDicToken != null && BaseWebApiExt.StaffDicToken.Count() > 0)
                {
                    if (BaseWebApiExt.StaffDicToken.ContainsValue(tRealitytoken))
                    {
                        return true;
                    }
                }
                //验证数据库是否存在
                var t = SLContract.Entities.ToList();
                var staffData = SLContract.Entities.Where(d => d.Token == tRealitytoken).FirstOrDefault();
                if (staffData != null)
                {
                    if (BaseWebApiExt.GetStaffByToken(tRealitytoken, staffData))
                    {
                        //BaseWebApiController.GetNewToken(UserContract,userData.Id);
                        return true;
                    }
                    else
                        return false;
                }
                else
                {
                    return false;
                }
            }
            else//User验证
            {
                var ULContract = ServiceProvider.GetService<IUsersLoginContract>();
                token = token.Replace(' ', '+');
                string tRealitytoken = "";
                try
                {
                    tRealitytoken = BaseWebApiExt.GetRealityToken(token);//解密
                }
                catch (Exception)
                {
                    return false;
                }

                //验证内存中是否存在
                if (BaseWebApiExt.DicToken != null && BaseWebApiExt.DicToken.Count() > 0)
                {
                    if (BaseWebApiExt.DicToken.ContainsValue(tRealitytoken))
                    {
                        return true;
                    }
                }
                //验证数据库是否存在
                var t = ULContract.Entities.ToList();
                var userData = ULContract.Entities.Where(d => d.Token == tRealitytoken).FirstOrDefault();
                if (userData != null)
                {
                    if (BaseWebApiExt.GetUserByToken(tRealitytoken, userData))
                    {
                        //BaseWebApiController.GetNewToken(UserContract,userData.Id);
                        return true;
                    }
                    else
                        return false;
                }
                else
                {
                    return false;
                }
            }
            

            //base64的字符串
            //字符串会转成一个byte[]
            //结构是 uid + uname + token串
            //去数据库中验证，合法后，刷新token，返回给客户，并且保存到数据库

        }
    }
}